package android.gpstrac.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.gpstrac.R;
import android.gpstrac.data.User;
import android.gpstrac.data.User.UserContent;
import android.gpstrac.data.UserLocation;
import android.gpstrac.data.UserLocation.UserLocationContent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GPSTracMap extends MapActivity {

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		final MapView map = (MapView)findViewById(R.id.map_view);
		map.setBuiltInZoomControls(true);
	}
	
	public void onStart()
	{
		super.onStart();
		
		final MapView map = (MapView)findViewById(R.id.map_view);
		Cursor cur = getContentResolver().query(UserContent.CONTENT_URI, null, null, null, null);
		int unameCol = cur.getColumnIndex(UserContent.NAME);
		
		ArrayList<UserLocation> locs = new ArrayList<UserLocation>();
		
		if(cur.moveToFirst())
		{
			int i = 0;
			do
			{
				Cursor loc = getContentResolver().query(
						UserLocationContent.CONTENT_URI, 
						null, 
						UserLocationContent.USER + "='" + cur.getString(unameCol) + "'", 
						null, 
						UserLocationContent.SEQ + " DESC");
				
				if(loc.moveToFirst())
				{
					int userCol = loc.getColumnIndex(UserLocationContent.USER);
					int latCol = loc.getColumnIndex(UserLocationContent.LAT);
					int lonCol = loc.getColumnIndex(UserLocationContent.LONG);
					int seqCol = loc.getColumnIndex(UserLocationContent.SEQ);
					locs.add(new UserLocation(
							new User(loc.getString(userCol)), 
							loc.getDouble(latCol),
							loc.getDouble(lonCol),
							false,
							loc.getInt(seqCol)));
				}
			}while(cur.moveToNext());
		}
		
		Drawable pin = this.getResources().getDrawable(R.drawable.map_pin);
		pin.setBounds(0, 0, pin.getIntrinsicWidth(), pin.getIntrinsicHeight());
		ArrayList<FriendOverlay> overlays = new ArrayList<FriendOverlay>();
		for(int i = 0; i < locs.size(); i++)
		{
			if(!locs.get(i).isInvalid())
				overlays.add(new FriendOverlay(
								new GeoPoint((int)(locs.get(i).getLat() * 1E6), (int)(locs.get(i).getLon() * 1E6)),
								locs.get(i).getUser().getName(),
								"" + locs.get(i).getLat() + ", " + locs.get(i).getLon()));
		}
		
		
		FriendItemizedOverlay iOver = new FriendItemizedOverlay(overlays, pin);
		List<Overlay> mapOverlays = map.getOverlays();
		
		mapOverlays.add(iOver);
		map.invalidate();
	}
	
	public void onStop()
	{
		super.onStop();
		
		final MapView map = (MapView)findViewById(R.id.map_view);
		for(int i = 0; i < map.getOverlays().size(); i++)
			map.getOverlays().remove(i);
	}
	
	/* (non-Javadoc)
	 * @see com.google.android.maps.MapActivity#isRouteDisplayed()
	 */
	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}
	
	private class FriendItemizedOverlay extends ItemizedOverlay<FriendOverlay>
	{
		private ArrayList<FriendOverlay> mOverlays;
		
		public FriendItemizedOverlay(ArrayList<FriendOverlay> overlays, Drawable draw)
		{
			super(draw);			

			mOverlays = overlays;
			
			populate();
		}

		/* (non-Javadoc)
		 * @see com.google.android.maps.ItemizedOverlay#createItem(int)
		 */
		@Override
		protected FriendOverlay createItem(int index)
		{
			return mOverlays.get(index);
		}

		/* (non-Javadoc)
		 * @see com.google.android.maps.ItemizedOverlay#size()
		 */
		@Override
		public int size()
		{
			return mOverlays.size();
		}
		
		@Override
		protected boolean onTap(int index)
		{
			Intent contIntent = new Intent(GPSTracFriendHistApp.class.getName());
			contIntent.putExtra("wUserHist", mOverlays.get(index).getTitle());
			
			startActivity(contIntent);
			
			return true;
		}
	}
	
	private class FriendOverlay extends OverlayItem
	{		
		public FriendOverlay(GeoPoint point, String title, String snippet)
		{			
			super(point, title, snippet);
		}
	}
}
